package com.funsport.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.funsport.dto.request.SubmitFeedbackRequest;
import com.funsport.entity.User;
import com.funsport.entity.UserFeedback;
import com.funsport.mapper.UserFeedbackMapper;
import com.funsport.service.EmailService;
import com.funsport.service.IUserFeedbackService;
import com.funsport.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 用户反馈服务实现类
 *
 * @author Fun-Sport Team
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserFeedbackServiceImpl extends ServiceImpl<UserFeedbackMapper, UserFeedback> 
    implements IUserFeedbackService {
    
    private final EmailService emailService;
    private final IUserService userService;
    
    @Override
    public void submitFeedback(Long userId, SubmitFeedbackRequest request) {
        log.info("用户 {} 提交反馈，类型: {}", userId, request.getType());
        
        // 保存反馈到数据库
        UserFeedback feedback = new UserFeedback();
        feedback.setUserId(userId);
        feedback.setType(request.getType());
        feedback.setContent(request.getContent());
        feedback.setContact(request.getContact());
        feedback.setImages(request.getImages());
        feedback.setStatus("pending");
        this.save(feedback);
        
        log.info("反馈已保存，ID: {}", feedback.getId());
        
        // 异步发送邮件通知
        try {
            User user = userService.getById(userId);
            emailService.sendFeedbackEmail(
                userId,
                user != null ? user.getNickname() : "未知用户",
                request.getType(),
                request.getContent(),
                request.getContact(),
                request.getImages()
            );
            log.info("反馈邮件已发送");
        } catch (Exception e) {
            log.error("发送反馈邮件失败", e);
            // 邮件发送失败不影响反馈提交
        }
    }
}

